package com.example.weight.arrayAdapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.weight.R;

import java.util.List;

public class ItemAdapter extends ArrayAdapter<Item> {
    private int resourceId;
    private List<Item> list;
    private int deletePos = -1;

    // 适配器的构造函数，把要适配的数据传入这里
    public ItemAdapter(Context context, int textViewResourceId, List<Item> objects){
        super(context,textViewResourceId,objects);
        resourceId = textViewResourceId;
        list = objects;
    }

    // convertView 参数用于将之前加载好的布局进行缓存
    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        Item item = getItem(position); //获取当前项的item实例

        // 加个判断，以免ListView每次滚动时都要重新加载布局，以提高运行效率
        View view;
        ViewHolder viewHolder;
        if (convertView==null){

            // 避免ListView每次滚动时都要重新加载布局，以提高运行效率
            view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);

            // 避免每次调用getView()时都要重新获取控件实例
            viewHolder=new ViewHolder();
            viewHolder.itemImage=view.findViewById(R.id.item_image);
            viewHolder.itemName=view.findViewById(R.id.item_name);

            // 将ViewHolder存储在View中（即将控件的实例存储在其中）
            view.setTag(viewHolder);
        } else{
            view=convertView;
            viewHolder=(ViewHolder) view.getTag();
        }

        // 获取控件实例，并调用set...方法使其显示出来
        viewHolder.itemImage.setImageResource(item.getImageId());
        viewHolder.itemImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                remove(getItem(position));
                deletePos = position;
            }
        });
        viewHolder.itemName.setText(item.getName());
        return view;
    }

    // 定义一个内部类，用于对控件的实例进行缓存
    class ViewHolder{
        ImageView itemImage;
        TextView itemName;
    }

    public List<Item> getList() {
        return list;
    }

    public int getDeletePos() {
        return deletePos;
    }
}
